
//  BLHUD.m
//  MReader
//
//  Created by 乔小乔 on 2020/4/12.
//  Copyright © 2020 Qi Liu. All rights reserved.
//

#import "BLHUD.h"

static BLHUD *shareHud=nil;

@implementation BLHUD
+(BLHUD *)shareHud
{
    static dispatch_once_t predicate;
    dispatch_once(&predicate, ^{
        shareHud = [[self alloc] init];
    });
    return shareHud;
}

-(void)showLoadingView{
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    if (window) {
        _hud=[MBProgressHUD showHUDAddedTo:window animated:YES];
    }
    [_hud hide:YES afterDelay:10];
}

- (void)showLoadingTextView:(NSString *)text{
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    if (window) {
        _hud=[MBProgressHUD showHUDAddedTo:window animated:YES];
        _hud.detailsLabelText = text;
    }
    [_hud hide:YES afterDelay:10];
}

-(void)hideLoadingView{
    [_hud hide:YES];
}

-(void)showtip:(NSString*)mes{
    [self hideLoadingView];
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[UIApplication sharedApplication].keyWindow animated:YES];
    hud.userInteractionEnabled = YES;
    hud.backgroundColor = [UIColor clearColor];
    hud.animationType = MBProgressHUDAnimationZoomOut;
    hud.detailsLabelText = mes;
    hud.square = NO;
    hud.mode = MBProgressHUDModeText;
    hud.removeFromSuperViewOnHide = YES;
    [hud hide:YES afterDelay:1];
}

-(void)showtip:(NSString*)mes time:(NSInteger)integer{
    [self hideLoadingView];
      MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[UIApplication sharedApplication].keyWindow animated:YES];
      hud.userInteractionEnabled = YES;
      hud.backgroundColor = [UIColor clearColor];
      hud.animationType = MBProgressHUDAnimationZoomOut;
      hud.detailsLabelText = mes;
      hud.square = NO;
      hud.mode = MBProgressHUDModeText;
      hud.removeFromSuperViewOnHide = YES;
      [hud hide:YES afterDelay:integer];
}

-(void)showtipInView:(UIView *)view message:(NSString*)mes{
    [self hideLoadingView];
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    hud.userInteractionEnabled = YES;
    hud.backgroundColor = [UIColor clearColor];
    hud.animationType = MBProgressHUDAnimationZoomOut;
    hud.detailsLabelText = mes;
    hud.square = NO;
    hud.mode = MBProgressHUDModeText;
    hud.removeFromSuperViewOnHide = YES;
    [hud hide:YES afterDelay:1];
}

- (void)showAlertWithTitle:(NSString *)title
                   message:(NSString *)message
              showOtherBtn:(BOOL)show
               confirmTile:(NSString *)confirmTitle
                otherTitle:(NSString *)otherTitle
             confirmHandle:(MRAlertConfirmHandle)confirmHandle
              cancleHandle:(MRAlertCancleHandle)cancleHandle
{
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *cancleAction = [UIAlertAction actionWithTitle:otherTitle
                                                           style:UIAlertActionStyleDefault
                                                         handler:^(UIAlertAction * _Nonnull action) {
                                                             if (cancleHandle) {
                                                                 cancleHandle();
                                                             }
                                                         }];
    UIAlertAction *confirAction = [UIAlertAction actionWithTitle:confirmTitle
                                                           style:UIAlertActionStyleDefault
                                                         handler:^(UIAlertAction * _Nonnull action) {
                                                             if (confirmHandle) {
                                                                 confirmHandle();
                                                             }
                                                         }];
    if (show) {
        [alertVC addAction:cancleAction];
        [alertVC addAction:confirAction];
    } else {
        [alertVC addAction:confirAction];
    }
    if (![[self getCurrentViewController] isKindOfClass:[UIAlertController class]]) {
        [[self getCurrentViewController] presentViewController:alertVC animated:YES completion:nil];
    }
}

-(UIViewController *)getCurrentViewController
{
    UIWindow *window = [[UIApplication sharedApplication].delegate window];
    UIViewController *topViewController = [window rootViewController];
    
    while (true) {
        
        if (topViewController.presentedViewController) {
            
            topViewController = topViewController.presentedViewController;
            
        } else if ([topViewController isKindOfClass:[UINavigationController class]] && [(UINavigationController*)topViewController topViewController]) {
            
            topViewController = [(UINavigationController *)topViewController topViewController];
            
        } else if ([topViewController isKindOfClass:[UITabBarController class]]) {
            
            UITabBarController *tab = (UITabBarController *)topViewController;
            topViewController = tab.selectedViewController;
            
        } else {
            break;
        }
    }
    return topViewController;
}


@end
